package com.scmt.hmzd.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scmt.core.common.utils.ResultUtil;
import com.scmt.core.common.vo.PageVo;
import com.scmt.core.common.vo.Result;
import com.scmt.hmzd.entity.HmCoupon;
import com.scmt.hmzd.service.IHmCouponService;
import com.scmt.hmzd.wxLoginUtils.WxSecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(tags = "优惠券接口")
@RestController
@RequestMapping("/scmt/appMain")
public class AppCouponController {
    @Autowired
    private WxSecurityUtil securityUtil;
    @Autowired
    private IHmCouponService iHmCouponService;

    /**
     * 用于个人中心上展示优惠券个数
     * @return
     */
    @ApiOperation(value = "查看用户每种状态优惠券的个数", notes = "查看用户每种优惠券的个数")
    @GetMapping("/listCouponIds")
    public Result<Object> list() {
        String userId = securityUtil.getCurrentWxUser().getId();
        Map<String, Long> couponCount = iHmCouponService.getCouponCountByStatus(userId);
        return ResultUtil.data(couponCount);
    }



    @GetMapping("/getCouponList")
    @ApiOperation(value = "获取优惠券列表", notes = "获取优惠券列表")
    public Result<Object> getCouponList(PageVo page) {
        String userId = securityUtil.getCurrentWxUser().getId();
        IPage<HmCoupon> result = iHmCouponService.getCouponList(page, userId);
        return ResultUtil.data(result);
    }

    @GetMapping("/receive")
    @ApiOperation(value = "领取优惠券接口", notes = "领取优惠券接口")
    public Result<Object> receive(Long couponId) {
        String userId = securityUtil.getCurrentWxUser().getId();
        HmCoupon coupon = iHmCouponService.getById(couponId);
        iHmCouponService.receive(coupon, userId);
        // 领取优惠券成功
        return ResultUtil.data("优惠券领取成功！");
    }
}
